package com.wei800.turnover.config.druid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DynamicDataSourceConfig { 			// 动态数据源配置类
    @Bean("dataSource") 					// Bean标记名称
    @Primary                        // 注入时优先匹配
    @DependsOn({"vlaibaoDataSource", "vlaibao_dicDataSource", "vlaibao_websitetrafficDataSource"}) // 注入配置
    public DynamicDataSource getDataSource(
            @Autowired DataSource vlaibaoDataSource,
            @Autowired DataSource vlaibao_dicDataSource,
            @Autowired DataSource vlaibao_websitetrafficDataSource) { 	// 注入数据源
        Map<Object, Object> targetDataSources = new HashMap<>(5);// 定义Map集合
        // 使用DataSourceNames接口名称作为Map集合KEY，而Value保存的是注入的DataSource实例
        targetDataSources.put(DynamicDataSource.DataSourceNames.VLAIBAO_DATASOURCE,
                vlaibaoDataSource); 			// 保存数据源
        targetDataSources.put(DynamicDataSource.DataSourceNames.VLAIBAO_DIC_DATASOURCE,
                vlaibao_dicDataSource); 			// 保存数据源
        targetDataSources.put(DynamicDataSource.DataSourceNames.VLAIBAO_WEBSITETRAFFIC_DATASOURCE,
                vlaibao_websitetrafficDataSource); 			// 保存数据源
        return new DynamicDataSource(vlaibaoDataSource, targetDataSources);
    }
}

